import api from '@/api';

export default {
  namespace: 'supplier',

  state: {
    suppliers: {
      list: [],
      pagination: {},
    },
    accounts: {
      list: [],
      pagination: {}
    },
    contacts: {
      list: [],
      pagination: {}
    },
    bds: {
      list: [],
      pagination: {}
    },
    currentSupplier: {},
    incentiveState: [],
    incentiveList: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    *cosplay({ payload }, { call }) {
      return yield call(api.supplier.cosplay, payload)
    },
    *submitApply({ payload }, { call }) {
      return yield call(api.supplier.submitApply, payload)
    },
    *dealApply({ payload }, { call }) {
      return yield call(api.supplier.dealApply, payload)
    },
    *fetch({ payload }, { call, put }) {
      const response = yield call(api.supplier.query, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *fetchCurrent({ payload }, { call, put }) {
      const response = yield call(api.supplier.queryCurrent, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveCurrent',
          payload: response.data,
        });
      }
    },
    *fetchCompany({ payload }, { call, put }) {
      return yield call(api.supplier.queryCompany, payload);
    },
    *add({ payload }, { call }) {
      return yield call(api.supplier.create, payload);
    },
    *update({ payload }, { call, put }) {
      let res = yield call(api.supplier.update, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveCurrent',
          payload: res.data
        });
      }
      return res;
    },
    *delete({ payload }, { call }) {
      return yield call(api.supplier.remove, payload);
    },
    *addCompany({ payload }, { call }) {
      return yield call(api.supplier.updateCompany, payload);
    },
    *fetchAccounts({ payload }, { call, put }) {
      let res = yield call(api.supplier.queryAccounts, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveAccounts',
          payload: {
            list: res.data,
            pagination: res.pagination
          }
        });
      }
    },
    *lockAccount({ payload }, { call }) {
      return yield call(api.supplier.lockAccount, payload);
    },
    *unlockAccount({ payload }, { call }) {
      return yield call(api.supplier.unlockAccount, payload);
    },
    *addAccount({ payload }, { call }) {
      return yield call(api.supplier.createAccount, payload);
    },
    *updateAccount({ payload }, { call }) {
      return yield call(api.supplier.updateAccount, payload);
    },
    *resetPwd({ payload }, { call }) {
      return yield call(api.supplier.resetPwd, payload);
    },
    *removeAccounts({ payload }, { call }) {
      return yield call(api.supplier.removeAccounts, payload);
    },
    *fetchContacts({ payload }, { call, put }) {
      let res = yield call(api.supplier.queryContacts, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveContacts',
          payload: {
            list: res.data,
            pagination: res.pagination
          }
        });
      }
    },
    *addContact({ payload }, { call }) {
      return yield call(api.supplier.createContact, payload);
    },
    *updateContact({ payload }, { call }) {
      return yield call(api.supplier.updateContact, payload);
    },
    *removeContacts({ payload }, { call }) {
      return yield call(api.supplier.removeContacts, payload);
    },
    *fetchBds({ payload }, { call, put }) {
      let res = yield call(api.supplier.queryBd, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveBds',
          payload: {
            list: res.data,
            pagination: res.pagination
          }
        });
      }
    },
    *addBd({ payload }, { call }) {
      return yield call(api.supplier.createBd, payload);
    },
    *updateBd({ payload }, { call }) {
      return yield call(api.supplier.updateBd, payload);
    },
    *removeBds({ payload }, { call }) {
      return yield call(api.supplier.removeBd, payload);
    },
    *fetchBasicCompany({ payload }, { call, put }) {
      return yield call(api.supplier.queryBasicCompany, payload);
    },
    *addBasicCompany({ payload }, { call }) {
      return yield call(api.supplier.updateBasicCompany, payload);
    },
    *fetchServiceQuality({ payload }, { call, put }) {
      return yield call(api.supplier.queryServiceQuality, payload);
    },
    *updateServiceQuality({ payload }, { call, put }) {
      return yield call(api.supplier.updateServiceQuality, payload);
    },
    *fetchWarehouse({ payload }, { call, put }) {
      return yield call(api.supplier.queryWarehouse, payload);
    },
    *fetchServicePeriod({ payload }, { call, put }) {
      return yield call(api.supplier.queryServicePeriod, payload);
    },
    *updateServicePeriod({ payload }, { call, put }) {
      return yield call(api.supplier.updateServicePeriod, payload);
    },
    *downLoadAttachments({ payload }, { call, put }) {
      return yield call(api.supplier.downAttachments, payload);
    },
    *fetchIncentiveState({ payload }, { call, put }) {
      let res = yield call(api.supplier.incentive.state, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveIncentiveState',
          payload: res.data
        });
      }
    },
    *fetchIncentiveList({ payload }, { call, put }) {
      let res = yield call(api.supplier.incentive.list, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveIncentiveList',
          payload: {
            list: res.data,
            pagination: res.pagination
          }
        });
      }
      return res
    },
    *fetchSupplierByWaybillNo({ payload }, { call, put }) {
      return yield call(api.supplier.incentive.supplier, payload);
    },
    *updateIncentive({ payload }, { call, put }) {
      return yield call(api.supplier.incentive.update, payload);
    },
    *deleteIncentive({ payload }, { call, put }) {
      return yield call(api.supplier.incentive.delete, payload);
    },
    *queryImportResult({ payload }, { call, put }) {
      return yield call(api.supplier.incentive.queryImportResult, payload);
    },
    *saveImportResult({ payload }, { call, put }) {
      return yield call(api.supplier.incentive.saveImportResult, payload);
    },
  },

  reducers: {
    save(state, action) {
      const { data, pagination } = action.payload;
      return {
        ...state,
        suppliers: {
          list: data,
          pagination
        },
      };
    },
    saveCurrent(state, { payload }) {
      return {
        ...state,
        currentSupplier: payload
      };
    },
    clearCurrent(state) {
      return {
        ...state,
        currentSupplier: {}
      }
    },
    saveAccounts(state, { payload }) {
      return {
        ...state,
        accounts: payload
      };
    },
    saveContacts(state, { payload }) {
      return {
        ...state,
        contacts: payload
      };
    },
    saveBds(state, { payload }) {
      return {
        ...state,
        bds: payload
      };
    },
    saveIncentiveState(state, { payload }) {
      return {
        ...state,
        incentiveState: payload
      };
    },
    saveIncentiveList(state, { payload }) {
      return {
        ...state,
        incentiveList: payload
      };
    },
  }
};
